import java.util.Stack;

class Solution {
    public int evalRPN(String[] tokens) {
        Stack<Integer> stack = new Stack<Integer>();
        //1.遍历数组判断当中的字符串类型
        for (int i = 0; i < tokens.length; i++) {
            if (!isOperation(tokens[i])) {
                //是数字 - 将字符串中的字符转换成整数压入
                stack.push(Integer.parseInt(tokens[i]));
            } else {
                //数操作符
                //弹出两个栈顶的元素
                int num2 = stack.pop();
                int num1 = stack.pop();
                //计算
                switch (tokens[i]) {
                    case "+":
                        stack.push(num1 + num2);
                        break;
                    case "-":
                        stack.push(num1 - num2);
                        break;
                    case "*":
                        stack.push(num1 * num2);
                        break;
                    case "/":
                        stack.push(num1 / num2);
                        break;
                }
            }

        }
        return stack.pop();
    }

    private boolean isOperation(String s) {
        //比较数组当中字符是数字还是操作符
        if (s.equals("+") || s.equals("-") || s.equals("*") || s.equals("/")) {
            return true;
        } else {
            return false;
        }
    }
}

public class MyStackTest {
    public static void main(String[] args){
        Stack<Integer> stack = new Stack<Integer>();
        //往栈中压入4个元素
        stack.push(100);
        stack.push(200);
        stack.push(300);
        stack.push(400);

        //拿出栈顶元素用stackTop接受
        int stackTop  = stack.pop();
        System.out.println("栈顶元素："+  stackTop);//输出

        //获取栈顶元素但不删除 - 此时栈顶是300
        int get = stack.peek();
        System.out.println("栈顶元素：" + get);

        //判断当前栈是不是空的 - 空的返回true - 不是空的返回false
        System.out.println(stack.empty());
    }
}
